package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.mapper.NodeMapper;
import com.heima.mapper.VmTypeMapper;
import com.heima.pojo.dto.DeviceQueryDTO;
import com.heima.pojo.dto.NodeVmDTO;
import com.heima.pojo.dto.PartnerQueryDTO;
import com.heima.pojo.entity.*;
import com.heima.pojo.vo.SkuClassPageVO;
import com.heima.pojo.vo.VendingMachineVO;
import com.heima.service.VmTypeService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class VmTypeServiceImpl implements VmTypeService {
    @Autowired
    private VmTypeMapper vmTypeMapper;
    



    @Override
    public SkuClassPageVO<VmType> queryPage(PartnerQueryDTO dto) {
        // 设置默认分页参数
        int pageIndex = (dto.getPageIndex() == null || dto.getPageIndex() < 1) ? 1 : dto.getPageIndex();
        int pageSize = (dto.getPageSize() == null || dto.getPageSize() < 1) ? 10 : dto.getPageSize();
        
        // 使用PageHelper进行分页
        PageHelper.startPage(pageIndex, pageSize);
        List<VmType> records = vmTypeMapper.queryPage(dto);

        SkuClassPageVO skuClassPageVO = new SkuClassPageVO();
        skuClassPageVO.setPageSize(pageSize);
        skuClassPageVO.setPageIndex(pageIndex);
        // 获取分页信息
        Page<VmType> page = (Page<VmType>) records;
        skuClassPageVO.setTotalPage(page.getPages());
        skuClassPageVO.setTotalCount(page.getTotal());
        skuClassPageVO.setCurrentPageRecords(records);

        return skuClassPageVO; // 确保返回正确的对象
    }

    @Override
    public void add(VmType vmType) {
        vmTypeMapper.add(vmType);

    }

    @Override
    public Boolean update(Integer typeId, VmType vmType) {
        return vmTypeMapper.update(typeId, vmType) > 0;

    }

    @Override
    public Boolean detele(Integer typeId) {
        return vmTypeMapper.detele(typeId) > 0;
    }

    @Override
    public SkuClassPageVO<VendingMachineVO> searchAll(DeviceQueryDTO deviceQueryDTO) {
        PageHelper.startPage(deviceQueryDTO.getPageIndex(),deviceQueryDTO.getPageSize());
         List<VendingMachineVO> records=vmTypeMapper.searchAll(deviceQueryDTO);
        Page<VendingMachineVO> page = (Page<VendingMachineVO>) records;
         SkuClassPageVO skuClassPageVO = new SkuClassPageVO();
         skuClassPageVO.setPageIndex(deviceQueryDTO.getPageIndex());
         skuClassPageVO.setPageSize(deviceQueryDTO.getPageSize());
         skuClassPageVO.setTotalCount(page.getTotal());
         skuClassPageVO.setTotalPage(page.getPages());
         skuClassPageVO.setCurrentPageRecords(records);
         return skuClassPageVO;


    }

    @Override
    public Boolean addVm(NodeVmDTO nodeVmDTO) {
        Node node=vmTypeMapper.selectTbNode(nodeVmDTO.getNodeId());
        TbVendingMachine tbVendingMachine = new TbVendingMachine();
        // 不再复制id字段，让数据库自动生成
        tbVendingMachine.setInnerCode(node.getId().toString());
        tbVendingMachine.setVmType(nodeVmDTO.getVmType());
        tbVendingMachine.setNodeId(Long.valueOf(nodeVmDTO.getNodeId()));
        tbVendingMachine.setVmStatus(1);
        TbOrder tbOrder=vmTypeMapper.selectTbOrder(nodeVmDTO.getNodeId());
        
        if (tbOrder != null) {
            TbChannel tbChannel=vmTypeMapper.selectTbChannel(tbOrder.getSkuId());
            tbVendingMachine.setLastSupplyTime(tbChannel.getLastSupplyTime());
        }
        
        // 设置其他必需字段
        tbVendingMachine.setCityCode(node.getAreaCode());
        tbVendingMachine.setAreaId(Integer.valueOf(node.getAreaCode()));
        tbVendingMachine.setCreateUserId(node.getCreateUserId().longValue());
        tbVendingMachine.setCreateUserName("system"); // 可根据实际需求修改
        tbVendingMachine.setClientId(""); // 可根据实际需求修改
        tbVendingMachine.setLongitudes(0.0); // 可根据实际需求修改
        tbVendingMachine.setLatitude(0.0); // 可根据实际需求修改
        tbVendingMachine.setCreateTime(LocalDateTime.now());
        tbVendingMachine.setUpdateTime(LocalDateTime.now());
        tbVendingMachine.setBusinessId(node.getBusinessId());
        tbVendingMachine.setRegionId(node.getRegionId());
        tbVendingMachine.setOwnerId(node.getOwnerId());
        tbVendingMachine.setOwnerName(node.getOwnerName());
        tbVendingMachine.setLastSupplyTime(LocalDateTime.now());
        
        try {
            vmTypeMapper.insertTbVendingMachine(tbVendingMachine);
        } catch (Exception e) {
            log.info("错误"+e);
            return false;
        }
        return true;
    }

}